#include <gtk/gtk.h>   
#include <gst/gst.h>   
#include <signal.h>   
#include <string.h>   
#include <gst/interfaces/xoverlay.h>
#include <glade/glade.h>
#include <time.h>

#include "config.h"
#include "mp4_player.h"
#include "gif_player.h"
#include "jpg_player.h"
#include "socket_client.h"
#include "printer_local.h"
#include "message_handler.h"
#include "xml_client.h"
#include "sys_time.h"
#include "keyboard.h"
#include "sys_log.h"
#include "url_config_win.h"

static gint delete_event ( GtkWidget *widget, GdkEvent *event, gpointer data ) 
{ 
	SYS_LOG("delete event occerred"); 
	return(FALSE); 
} 

static void destroy( GtkWidget *widget, gpointer data ) 
{ 
} 

static GtkWindow *show_text_u_in_new_win(GtkWidget *view, char* text,int size) 
{
	gchar *markup;

	if(!text || !view) {
		SYS_LOG("show_text_in_new_win error");
		return NULL;
	}

	int str_len = strlen(text);
	markup=g_markup_printf_escaped("<span size=\"%d\" foreground=\"black\" >%s</span>",size,text);

	gtk_label_set_markup(GTK_LABEL(view),markup);
	g_free( (gpointer)markup );
	
	gtk_widget_show(view);

	return NULL;
}

static void
update_widget_bg(GtkWidget *widget, gchar *img_file, int w, int h)
{		
	GdkPixbuf *src_pixbuf;		
	GdkPixbuf *dst_pixbuf;	   
	GtkWidget	*image;
	gint width, height;    


	if(-1 == access(img_file,F_OK)){
		SYS_LOG ("file not exist");
		return;
	}
	src_pixbuf = gdk_pixbuf_new_from_file(img_file, NULL);	  
	width = gdk_pixbuf_get_width(src_pixbuf);	
	height = gdk_pixbuf_get_height(src_pixbuf);    

	if (src_pixbuf ==NULL){   
		  SYS_LOG ("pixbuf create failed");
		  return;
	}
	dst_pixbuf=gdk_pixbuf_scale_simple(src_pixbuf,w,h,GDK_INTERP_BILINEAR);

	gtk_image_set_from_pixbuf((GtkImage *)widget,dst_pixbuf);	 
	g_object_unref(src_pixbuf); 
	g_object_unref(dst_pixbuf);
	
}

static GtkWidget *new_widget_bg(GtkWidget *widget, gchar *img_file, int w, int h)
{		
	GdkPixbuf *src_pixbuf;		
	GdkPixbuf *dst_pixbuf;	   
	GtkWidget	*image;
	gint width, height;    


	if(-1 == access(img_file,F_OK)){
		SYS_LOG ("file not exist");
		return;
	}
	src_pixbuf = gdk_pixbuf_new_from_file(img_file, NULL);	  
	width = gdk_pixbuf_get_width(src_pixbuf);	
	height = gdk_pixbuf_get_height(src_pixbuf);    

	if (src_pixbuf ==NULL){   
		  SYS_LOG ("pixbuf create failed");
		  return;
	}
	dst_pixbuf=gdk_pixbuf_scale_simple(src_pixbuf,w,h,GDK_INTERP_BILINEAR);

	image=gtk_image_new();
	gtk_widget_set_size_request(image,w,h);
	gtk_image_set_from_pixbuf((GtkImage *)image,dst_pixbuf);	 
	g_object_unref(src_pixbuf); 
	g_object_unref(dst_pixbuf); 
    gtk_container_add(GTK_CONTAINER(widget),image);
	return image;
	
}

static GtkWidget *big_screen_gui_builder_set_left(GtkBuilder *builder,print_sys_config_s *sys_config)
{
	GtkFixed *fixed2 = GTK_FIXED(gtk_builder_get_object (builder, "fixed2"));

	//set left box bg
	GtkWidget *bg = GTK_WIDGET(gtk_builder_get_object (builder, "left_bg_img1"));
	gtk_fixed_move(GTK_FIXED(fixed2),bg,0,0);
	update_widget_bg(bg,sys_config->sys_config.image_bg,sys_config->bs_left_fix_w,sys_config->bs_left_fix_h);

	//set left box bg--text
	bg = GTK_WIDGET(gtk_builder_get_object (builder, "left_bg_img2"));
	gtk_fixed_move(GTK_FIXED(fixed2),bg,2*sys_config->bs_pad,4*sys_config->bs_pad+sys_config->bs_qr_img_h);
	update_widget_bg(bg,sys_config->sys_config.frame_bg,sys_config->bs_left_fix_w-4*sys_config->bs_pad,6*sys_config->bs_usr_text_h+3*sys_config->bs_pad);

//set
	//set qr
	sys_config->image_qr_code = GTK_WIDGET(gtk_builder_get_object (builder, "image_qr_code"));
	gtk_fixed_move(fixed2,sys_config->image_qr_code,(sys_config->bs_left_fix_w-sys_config->bs_qr_img_w)/2,2*sys_config->bs_pad);
	gtk_widget_set_size_request(sys_config->image_qr_code,sys_config->bs_qr_img_w,sys_config->bs_qr_img_h);
	update_widget_bg(sys_config->image_qr_code,sys_config->sys_config.image_qr,sys_config->bs_qr_img_w,sys_config->bs_qr_img_h);

	//set code
	bg = GTK_WIDGET(gtk_builder_get_object (builder, "label_code_pre"));
	gtk_fixed_move(fixed2,bg,(sys_config->bs_left_fix_w-sys_config->bs_qr_img_w)/2,2*sys_config->bs_pad+sys_config->bs_qr_img_h);
	show_text_u_in_new_win(bg,"微信验证码 :",15000);

	sys_config->label_code = GTK_WIDGET(gtk_builder_get_object (builder, "label_code"));
	gtk_fixed_move(fixed2,sys_config->label_code,(sys_config->bs_left_fix_w)/2+sys_config->bs_pad,2*sys_config->bs_pad+sys_config->bs_qr_img_h);
	//show_text_u_in_new_win(sys_config->label_code,sys_config->sys_config.code,10000);
    if (0 == strcmp(sys_config->sys_config.printer_mode,"free")) {
	    show_text_u_in_new_win(sys_config->label_code,sys_config->sys_config.code,15000);
    }
	//set usr text

	bg = GTK_WIDGET(gtk_builder_get_object (builder, "label_usr_info"));
	gtk_fixed_move(GTK_FIXED(fixed2),bg,(sys_config->bs_left_fix_w-sys_config->bs_qr_img_w)/2,4*sys_config->bs_pad+sys_config->bs_qr_img_h);
	show_text_u_in_new_win(bg,sys_config->sys_config.right_fixed_label_2,30000);

	sys_config->label_text_1= GTK_WIDGET(gtk_builder_get_object (builder, "label_usr_set1"));
	gtk_fixed_move(fixed2,sys_config->label_text_1,(sys_config->bs_left_fix_w-sys_config->bs_usr_text_w),4*sys_config->bs_pad+2*sys_config->bs_usr_text_h+sys_config->bs_qr_img_h);
	show_text_u_in_new_win(sys_config->label_text_1,sys_config->sys_config.right_label_1,20000);

	sys_config->label_text_2= GTK_WIDGET(gtk_builder_get_object (builder, "label_usr_set2"));
	gtk_fixed_move(fixed2,sys_config->label_text_2,(sys_config->bs_left_fix_w-sys_config->bs_usr_text_w),5*sys_config->bs_pad+3*sys_config->bs_usr_text_h+sys_config->bs_qr_img_h);	
	show_text_u_in_new_win(sys_config->label_text_2,sys_config->sys_config.right_label_2,20000);

	sys_config->label_text_3= GTK_WIDGET(gtk_builder_get_object (builder, "label_usr_set3"));
	gtk_fixed_move(fixed2,sys_config->label_text_3,(sys_config->bs_left_fix_w-sys_config->bs_usr_text_w),6*sys_config->bs_pad+4*sys_config->bs_usr_text_h+sys_config->bs_qr_img_h);
	show_text_u_in_new_win(sys_config->label_text_3,sys_config->sys_config.right_label_3,20000);

	sys_config->label_text_4= GTK_WIDGET(gtk_builder_get_object (builder, "label_usr_set4"));
	gtk_fixed_move(fixed2,sys_config->label_text_4,(sys_config->bs_left_fix_w-sys_config->bs_usr_text_w),7*sys_config->bs_pad+5*sys_config->bs_usr_text_h+sys_config->bs_qr_img_h);
	show_text_u_in_new_win(sys_config->label_text_4,sys_config->sys_config.right_label_4,20000);

	//set logo
	sys_config->image_logo= GTK_WIDGET(gtk_builder_get_object (builder, "image_logo"));
	gtk_fixed_move(fixed2,sys_config->image_logo,(sys_config->bs_left_fix_w-sys_config->bs_logo_img_w)/2,8*sys_config->bs_pad+6*sys_config->bs_usr_text_h+sys_config->bs_qr_img_h);
	gtk_widget_set_size_request(sys_config->image_logo,sys_config->bs_logo_img_w,sys_config->bs_logo_img_h);
	update_widget_bg(sys_config->image_logo,sys_config->sys_config.image_logo,sys_config->bs_logo_img_w,sys_config->bs_logo_img_h);

}

static GtkWidget *big_screen_gui_builder_set_right(GtkBuilder *builder,print_sys_config_s *sys_config)
{
	GtkFixed *fixed3 = GTK_FIXED(gtk_builder_get_object (builder, "fixed3"));

	//set right box bg
	GtkWidget *bg = GTK_WIDGET(gtk_builder_get_object (builder, "right_bg_img"));
	gtk_fixed_move(fixed3,bg,0,0);
	update_widget_bg(bg,sys_config->sys_config.image_bg,sys_config->bs_right_fix_w,sys_config->bs_right_fix_h);

	//set ad jpg or mp4

	if(0 == strcmp(sys_config->sys_config.screen_module,"big_screen_gui_jpg")){
		sys_config->image_ad_main = GTK_WIDGET(gtk_builder_get_object (builder, "image_ad_main"));
		gtk_fixed_move(fixed3,sys_config->image_ad_main,sys_config->bs_pad,sys_config->bs_pad);
		gtk_widget_set_size_request(sys_config->image_ad_main,sys_config->bs_main_ad_w,sys_config->bs_main_ad_h);
		update_widget_bg(sys_config->image_ad_main,sys_config->sys_config.main_ad,sys_config->bs_main_ad_w,sys_config->bs_main_ad_h);
	} else {
		sys_config->frame_movie = GTK_WIDGET(gtk_builder_get_object (builder, "framemovie"));
		gtk_fixed_move(fixed3,sys_config->frame_movie,sys_config->bs_pad,sys_config->bs_pad);
		gtk_widget_set_size_request(sys_config->frame_movie,sys_config->bs_main_ad_w,sys_config->bs_main_ad_h);
	}
	

	//set device_id
	sys_config->device_id_text = GTK_WIDGET(gtk_builder_get_object (builder, "device_id"));
	gtk_fixed_move(fixed3,sys_config->device_id_text, sys_config->bs_right_fix_w/2 - sys_config->bs_pad,sys_config->bs_main_ad_h+sys_config->bs_pad);
	show_text_u_in_new_win(sys_config->device_id_text,sys_config->sys_config.device_id,10000);


//end

}


GtkWidget *big_screen_gui_builder(print_sys_config_s *sys_config)  
{  
	GtkBuilder *builder;
	GtkWidget *window = NULL; 

	builder = gtk_builder_new();
	gtk_builder_add_from_file(builder,"./res/big_screen_gui.glade", NULL );
	window = GTK_WIDGET(gtk_builder_get_object (builder, "main_win"));
	gtk_builder_connect_signals(builder, NULL );

	if(!window)
	SYS_LOG ("window null\n");

	gtk_window_set_default_size(GTK_WINDOW(window), sys_config->screen_w, sys_config->screen_h);
    gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
	gtk_window_fullscreen(GTK_WINDOW(window));


	/*set screen value*/
	sys_config->bs_pad = 20;
	sys_config->bs_frame_img_w=sys_config->screen_w*0.95;
	sys_config->bs_frame_img_h=sys_config->screen_h*0.95;
	sys_config->bs_left_fix_w=sys_config->bs_frame_img_w*0.25;
	sys_config->bs_left_fix_h=sys_config->bs_frame_img_h-2*sys_config->bs_pad;
	sys_config->bs_right_fix_w=sys_config->bs_frame_img_w-sys_config->bs_left_fix_w-2.5*sys_config->bs_pad;
	sys_config->bs_right_fix_h=sys_config->bs_left_fix_h;
	sys_config->bs_qr_img_w=sys_config->bs_left_fix_w/2;
	sys_config->bs_qr_img_h=sys_config->bs_left_fix_w/2;
	sys_config->bs_logo_img_w=sys_config->bs_left_fix_w/2;
	sys_config->bs_logo_img_h=sys_config->bs_left_fix_w/4;
	sys_config->bs_usr_text_w=sys_config->bs_left_fix_w-4*sys_config->bs_pad;
	sys_config->bs_usr_text_h=(sys_config->bs_left_fix_h-5*sys_config->bs_pad-sys_config->bs_qr_img_h-sys_config->bs_logo_img_h)/8;
	sys_config->bs_main_ad_w=sys_config->bs_right_fix_w-2*sys_config->bs_pad;
	sys_config->bs_main_ad_h=sys_config->bs_right_fix_h-2*sys_config->bs_pad;
	sys_config->bs_text_machine_id_h=sys_config->bs_pad;
	//set end
	SYS_LOG("sys_time_config->bs_pad[%d]",sys_config->bs_pad);
	SYS_LOG("sys_time_config->bs_frame_img_w[%d]",sys_config->bs_frame_img_w);
	SYS_LOG("sys_time_config->bs_frame_img_h[%d]",sys_config->bs_frame_img_h);
	SYS_LOG("sys_time_config->bs_left_fix_w[%d]",sys_config->bs_left_fix_w);
	SYS_LOG("sys_time_config->bs_left_fix_h[%d]",sys_config->bs_left_fix_h);
	SYS_LOG("sys_time_config->bs_right_fix_w[%d]",sys_config->bs_right_fix_w);
	SYS_LOG("sys_time_config->bs_right_fix_h[%d]",sys_config->bs_right_fix_h);
	SYS_LOG("sys_time_config->bs_qr_img_w[%d]",sys_config->bs_qr_img_w);
	SYS_LOG("sys_time_config->bs_qr_img_h[%d]",sys_config->bs_qr_img_h);
	SYS_LOG("sys_time_config->bs_usr_text_w[%d]",sys_config->bs_usr_text_w);
	SYS_LOG("sys_time_config->bs_usr_text_h[%d]",sys_config->bs_usr_text_h);
	SYS_LOG("sys_time_config->bs_logo_img_w[%d]",sys_config->bs_logo_img_w);
	SYS_LOG("sys_time_config->bs_logo_img_h[%d]",sys_config->bs_logo_img_h);
	SYS_LOG("sys_time_config->bs_main_ad_w[%d]",sys_config->bs_main_ad_w);
	SYS_LOG("sys_time_config->bs_main_ad_h[%d]",sys_config->bs_main_ad_h);


	GtkFixed *fixed1 = GTK_FIXED(gtk_builder_get_object (builder, "fixed1"));
	GtkWidget *main_box = GTK_WIDGET(gtk_builder_get_object (builder, "main_box"));
	gtk_fixed_move(fixed1,main_box,(sys_config->screen_w-sys_config->bs_frame_img_w)/2+sys_config->bs_pad,(sys_config->screen_h-sys_config->bs_frame_img_h)/2+sys_config->bs_pad);


//set bg
	GtkWidget *bg = GTK_WIDGET(gtk_builder_get_object (builder, "main_bg"));
	update_widget_bg(bg,sys_config->sys_config.main_bg,sys_config->screen_w,sys_config->screen_h);


	bg = GTK_WIDGET(gtk_builder_get_object (builder, "frame_bg"));
	gtk_fixed_move(fixed1,bg,(sys_config->screen_w-sys_config->bs_frame_img_w)/2,(sys_config->screen_h-sys_config->bs_frame_img_h)/2);
	update_widget_bg(bg,sys_config->sys_config.frame_bg,sys_config->bs_frame_img_w,sys_config->bs_frame_img_h);

//set photo bg
	sys_config->image_photo= GTK_WIDGET(gtk_builder_get_object (builder, "image_photo"));
	gtk_fixed_move(GTK_FIXED(fixed1),sys_config->image_photo,0,0);
	gtk_widget_set_size_request(sys_config->image_photo,sys_config->screen_w,sys_config->screen_h);
	update_widget_bg(sys_config->image_photo,sys_config->sys_config.image_photo_default,sys_config->screen_w,sys_config->screen_h);

//wifi logo
	//sys_config->image_wifi = GTK_WIDGET(gtk_builder_get_object (builder, "wifi_logo"));


//set button
	bg= GTK_WIDGET(gtk_builder_get_object (builder, "wifi_button"));
	gtk_widget_set_size_request(bg,40,40);
	gtk_fixed_move(fixed1,bg,0,sys_config->screen_h-55);
//   	gtk_fixed_move(fixed1,bg,(sys_config->screen_w-40)/2,0);
	sys_config->image_wifi = new_widget_bg(bg,sys_config->sys_config.image_wifi_off,40,40);
	g_signal_connect(G_OBJECT(bg), "clicked",G_CALLBACK(show_key_wifi_in_new_win), NULL);//set keyboard


//message win
    create_message_win(sys_config,builder);


//set config
    create_config_win(sys_config,builder);
    bg= GTK_WIDGET(gtk_builder_get_object (builder, "config_bt"));
    gtk_widget_set_size_request(bg,10,10);
    gtk_fixed_move(GTK_FIXED(fixed1),bg,sys_config->screen_w-25,sys_config->screen_h-25);
    gtk_button_set_relief(bg,GTK_RELIEF_NONE);
    g_signal_connect(G_OBJECT(bg), "clicked",G_CALLBACK(show_config_win), sys_config);//set keyboard

//set config sound
    create_config_win(sys_config,builder);
    bg= GTK_WIDGET(gtk_builder_get_object (builder, "config_sound"));
    gtk_widget_set_size_request(bg,10,10);
    gtk_fixed_move(GTK_FIXED(fixed1),bg,sys_config->screen_w-35,10);
    new_widget_bg(bg,sys_config->sys_config.image_sound,20,20);
    //gtk_button_set_relief(bg,GTK_RELIEF_NONE);
    g_signal_connect(G_OBJECT(bg), "clicked",G_CALLBACK(show_config_sound_win), sys_config);//set keyboard

//build screen
	big_screen_gui_builder_set_left(builder,sys_config);
	big_screen_gui_builder_set_right(builder,sys_config);
	gtk_widget_show_all(window);

	//set jpg player
	play_jpg_player_set_image(sys_config->image_ad_main,sys_config->image_ad_left,sys_config->image_ad_middle,sys_config->image_ad_right);
	play_jpg_player();

	//set mp4 player
	player_play_mp4(sys_config->frame_movie);
	
	return window;  
}



